﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using SlimDX.Direct3D11;
using SlimDX;

namespace ParticlesEngine
{
    public class ParticlesSystem
    {
        static int MAX = 65536;
        Stack<int> available = new Stack<int>(MAX);
        Texture2D position;
        Texture2D velocity;
        Vector4[] particlesPositions = new Vector4[MAX];
        Texture2DDescription description;
        Device device;

        public ParticlesSystem(Device device)
        {

            this.device = device;
            description = new Texture2DDescription();
            description.Format = SlimDX.DXGI.Format.R32G32B32A32_Float;       
            description.BindFlags = BindFlags.ShaderResource | BindFlags.RenderTarget;
            description.Width = 256;
            description.Height = 256;
            description.MipLevels = 1;
            description.ArraySize = 1;
            description.OptionFlags = ResourceOptionFlags.None;
            description.Usage = ResourceUsage.Default;
            description.SampleDescription = new SlimDX.DXGI.SampleDescription(1, 0);
            
            for(int i = MAX-1;i>=0;i--)
            {
                available.Push(i);
            }
        }

        public void CreateParticle(Vector4 initialPosition, Vector4 initialVelocity)
        {

            particlesPositions[available.Pop()] = initialPosition;
            DataStream positions = new DataStream(particlesPositions, true, true);            
            position = new Texture2D(this.device, description, new DataRectangle(4096,positions));
            
        }
    }
}
